package com.turkcell.dv.rest;

import java.util.ArrayList;
import java.util.List;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.turkcell.dv.dao.PrivilegeDAO;
import com.turkcell.dv.model.PrivilegeDOM;
import com.turkcell.dv.rest.dto.PrivilegeDTO;

@RestController
@RequestMapping(value = "/PrivilegeService", headers = "Content-Type=application/json", consumes = "application/json", produces = "application/json")
public class PrivilegeService {

	@Autowired	private PrivilegeDAO privilegeDAO;

	@RequestMapping(method = RequestMethod.POST)
	public PrivilegeDOM create(@RequestBody @Valid PrivilegeDOM privilege) {
		return privilegeDAO.addPrivilege(privilege);
	}

	@RequestMapping(method = RequestMethod.GET)
	public List<PrivilegeDTO> list() {
		List<PrivilegeDOM> pList = privilegeDAO.listPrivileges();
		ArrayList<PrivilegeDTO> privilegeDTOList = new ArrayList<PrivilegeDTO>();
		for (int i = 0; i < pList.size(); i++) {
			PrivilegeDTO pDTO = new PrivilegeDTO();
			pDTO.transformDOM(pList.get(i));
			privilegeDTOList.add(pDTO);
		}
		return privilegeDTOList;
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	public PrivilegeDOM get(@PathVariable("id") long id) {
		return privilegeDAO.getPrivilegeById(id);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
	public PrivilegeDOM update(@PathVariable("id") long id, @RequestBody @Valid PrivilegeDOM privilege) {
		return privilegeDAO.updatePrivilege(privilege);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	public ResponseEntity<Boolean> delete(@PathVariable("id") long id) {
		privilegeDAO.removePrivilege(id);
		return new ResponseEntity<Boolean>(Boolean.TRUE, HttpStatus.OK);
	}
}
